package com.market.cart.server.controller;

import java.nio.file.Path;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.market.common.log.annotation.Log;
import com.market.common.log.enums.BusinessType;
import com.market.common.security.annotation.RequiresPermissions;
import com.market.cart.common.domain.CartInfo;
import com.market.cart.server.service.ICartInfoService;
import com.market.common.core.web.controller.BaseController;
import com.market.common.core.utils.poi.ExcelUtil;
import com.market.common.core.web.page.TableDataInfo;
import com.market.common.core.domain.Result;

/**
 * 购物车Controller
 *
 * @author market
 * @date 2024-02-29
 */
@RestController
@RequestMapping("/cart")
public class CartInfoController extends BaseController
{
    @Autowired
    private ICartInfoService cartInfoService;

    /**
     * 查询购物车列表
     */
    @RequiresPermissions("cart:cart:list")
    @GetMapping("/list")
    public Result<TableDataInfo> list(CartInfo cartInfo)
    {
        startPage();
        List<CartInfo> list = cartInfoService.selectCartInfoList(cartInfo);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @RequiresPermissions("cart:cart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CartInfo cartInfo)
    {
        List<CartInfo> list = cartInfoService.selectCartInfoList(cartInfo);
        ExcelUtil<CartInfo> util = new ExcelUtil<CartInfo>(CartInfo.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @RequiresPermissions("cart:cart:query")
    @GetMapping(value = "/{cartInfoId}")
    public Result getInfo(@PathVariable("cartInfoId") Integer cartInfoId)
    {
        return success(cartInfoService.selectCartInfoByCartInfoId(cartInfoId));
    }

    /**
     * 新增购物车
     */
    @RequiresPermissions("cart:cart:add")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody CartInfo cartInfo)
    {
        return toAjax(cartInfoService.insertCartInfo(cartInfo));
    }

    /**
     * 修改购物车
     */
    @RequiresPermissions("cart:cart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody CartInfo cartInfo)
    {
        return toAjax(cartInfoService.updateCartInfo(cartInfo));
    }

    /**
     * 删除购物车
     */
    @RequiresPermissions("cart:cart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cartInfoIds}")
    public Result remove(@PathVariable Integer[] cartInfoIds)
    {
        return toAjax(cartInfoService.deleteCartInfoByCartInfoIds(cartInfoIds));
    }




    /*
    * 多选结算功能
    * */
    @PostMapping("/MultiSettleCart/{ids}/{token}")
    public Result MultiSettleCart(@PathVariable("ids") Integer[] ids,@PathVariable("token") String token ){
        return cartInfoService.MultiSettleCart(ids,token);
    }

    /*
    * 生成uuid
    * */
    @GetMapping("/getUUID")
    public Result getUUID(){
        return cartInfoService.getUUID();
    }



}
